//
//  SceneryDetailViewController.m
//  UIViewControllerTransitionDemo
//
//  Created by Guo Rui on 2017/7/4.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "SceneryDetailViewController.h"
#import "PushViewController.h"
#import "PopAnimator.h"
#import "PushAnimator.h"
#import "PopInteractiveAnimator.h"

@interface SceneryDetailViewController ()
@property (weak, nonatomic) IBOutlet UITextView *detailIntroTextView;
@property (strong, nonatomic) PopInteractiveAnimator *interactiveAnimator;
@end

@implementation SceneryDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.title = self.place;
    
    self.detailImageView.image = self.image;
    self.detailIntroTextView.text = self.introduce;
    
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    CGFloat imageView_width = screenSize.width - 2 * 10.f;
    self.detailImageView.frame = CGRectMake(10.f, 74.f, imageView_width, imageView_width * self.image.size.height / self.image.size.width);
    
    self.detailIntroTextView.frame = CGRectMake(CGRectGetMinX(self.detailImageView.frame), CGRectGetMaxY(self.detailImageView.frame) + 20.f, CGRectGetWidth(self.detailImageView.frame), screenSize.height - CGRectGetMaxY(self.detailImageView.frame) - 20.f - 10.f);

    
    self.interactiveAnimator = [[PopInteractiveAnimator alloc] initWithViewController:self];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.delegate = self;
}


/*
 *  1. 如果动画控制器和交互控制器同时返回，只执行交互控制器的动画。
    2. 如果只返回当中的一个，则执行返回的控制器的动画。
    3. 如果都没有返回，执行系统默认的动画。
 */
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController *)fromVC
                                                 toViewController:(UIViewController *)toVC {
    
    if (operation == UINavigationControllerOperationPop) {
        return [[PopAnimator alloc] init];
    }
    
    return nil;
}


- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController {
    return self.interactiveAnimator.interactiving ? self.interactiveAnimator : nil;
}


- (void)dealloc {
    
    NSLog(@"\n************\n %@ dealloc \n************", self.class);
}
@end
